/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset

* Sample
keep if sample_int==1 & eis_f!=. // keep observations of interest


* ---------
* Table A22
* ---------

* Globals
global dep "adj_gapportwei htrl_f_int adj_htrl_f_int_portwei htrl_f_ext adj_htrl_f_ext_portwei htrl_f_adv"
global controls "tax_rate risk red_i mixed_dec differentiated valitrade_advice"
global controls2 "tax_rate mixed_dec differentiated"

* Matrix
** Panel A
mat tableA22_1 = J(10,6,.)
mat tableA22_1T = J(1,6,.)
** Panel B
mat tableA22_2 = J(8,6,.)
mat tableA22_2T = J(1,6,.)

* Loop for estimations
** Panel A
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' eis_f $controls countryorig* max_twodigit* if sample_reg==1 , absorb(insp_f key_dec ym_*) vce(cluster insp_f key_dec)

	mat tableA22_1[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat tableA22_1[`i',`j'] = _se[eis_f]
	mat tableA22_1T[1,`j'] = e(N)
	local i = `i' + 8
	mat tableA22_1[`i',`j'] = e(r2)
	local i = `i' -9
	local j = `j' +1
}
** Panel B
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' eis_f $controls2 countryorig* max_twodigit* if sample_reg==1 & year_reg!=2018 , absorb(ym_*) vce(cluster insp_f key_dec)

	mat tableA22_2[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat tableA22_2[`i',`j'] = _se[eis_f]
	mat tableA22_2T[1,`j'] = e(N)
	local i = `i' + 6
	mat tableA22_2[`i',`j'] = e(r2)
	local i = `i' -7
	local j = `j' +1
}


* Export
* ------
putexcel set "$tables\Table A22.xlsx", replace sheet(TableA22) // create a new excel spreadsheet

* Title
putexcel (A1:G1), merge hcenter vcenter
putexcel A1 = "Before delegated randomization of inspector assignment"

* Dependent variables
putexcel A2 = "Dependent variable:", left vcenter
putexcel B2 = "Weight gap", hcenter vcenter bold
putexcel (C2:G2), merge hcenter vcenter bold
putexcel C2 = "Hyp. tax revenue losses"

* Reference price
putexcel A3 = "reference price", left vcenter
putexcel (C3:D3), merge hcenter vcenter
putexcel C3 = "Importer"
putexcel (E3:F3), merge hcenter vcenter
putexcel E3 = "Exporter"
putexcel G3 = "Third-party"

* Weight correction
putexcel A4 = "weight correction", left vcenter
putexcel C4 = "No", hcenter vcenter
putexcel D4 = "Yes", hcenter vcenter
putexcel E4 = "No", hcenter vcenter
putexcel F4 = "Yes", hcenter vcenter

* Panel A
** Name
putexcel (A6:G6), merge hcenter vcenter bold
putexcel A6 = "A. Differential Treatment by Inspectors"
** Coefficients
putexcel B8 = matrix(tableA22_1), nformat(0.000) hcenter vcenter
putexcel B16 = matrix(tableA22_1T), nformat(number_sep) hcenter vcenter
** Independent variables
putexcel A8 = "Excess interaction share"
local j = 10
local labels `" "Declaration characteristics" "Inspector fixed effects" "Broker fixed effects" "Source country fixed effects" "HS2-product fixed effects" "Month-year fixed effects" "Observations" "R-squared" "'
forvalues i = 1(1)8 {

	local x: word `i' of `labels'

	putexcel A`j' = "`x'", left vcenter
	local j = `j'+1
}

* Panel B
** Name
putexcel (A19:G19), merge hcenter vcenter bold
putexcel A19 = "B. Overall Revenue Losses Associated with Corruption"
** Coefficients
putexcel B21 = matrix(tableA22_2), nformat(0.000) hcenter vcenter
putexcel B27 = matrix(tableA22_2T), nformat(number_sep) hcenter vcenter
** Independent variables
putexcel A21 = "Excess interaction share"
local j = 23
local labels `" "Exogenous declaration characteristics" "Source country fixed effects" "HS2-product fixed effects" "Month-year fixed effects" "Observations" "R-squared" "'
forvalues i = 1(1)6 {

	local x: word `i' of `labels'

	putexcel A`j' = "`x'", left vcenter
	local j = `j'+1
}

* Column numbers
local cells `" "B" "C" "D" "E" "F" "G" "B" "C" "D" "E" "F" "G" "'
forvalues i = 1(1)12 {

	local y: word `i' of `cells'

	if `i' < 7 {
		putexcel `y'7 = "(`i')", hcenter vcenter
	}

	if `i' > 6 {
		putexcel `y'20 = "(`i')", hcenter vcenter
	}
}

* FE
local cells `" "B" "C" "D" "E" "F" "G" "'
forvalues i = 1(1)6 {
	
	local y: word `i' of `cells'
	
	forvalues j = 10(1)15 {
		putexcel `y'`j' = "Yes", hcenter vcenter
	}
	
	forvalues j = 23(1)26 {
		putexcel `y'`j' = "Yes", hcenter vcenter
	}
}


* -------------------------------- ENDS HERE -------------------------------- *